/*
 * Created by JFormDesigner on Wed Nov 30 15:31:23 EET 2011
 */

package tr.gov.tubitak.bilgem.esya.certviewer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateParsingException;
import java.util.ArrayList;
import java.util.ResourceBundle;

/**
 * @author Haydar Karabas
 */
public class ECertViewerDialog extends JDialog {
    public ECertViewerDialog(Frame owner) {
        super(owner);
        initComponents();
        initGUI();
    }

    public static void showCertificate(String certPath) throws IOException, CertificateParsingException {
        byte [] asnData=null;
        asnData =ECertUtil.getDERCertDataFromFile(new File(certPath));
        showCertificate(asnData);
    }

    public void addTabPage(String tabTitle,JPanel tabPage)
    {
        eCertViewer1.addTab(tabTitle,tabPage);
    }

    public static void showCertificate(byte [] certData)
    {
        ECertViewerDialog certShowDlg = new ECertViewerDialog(null,certData);
        certShowDlg.setModalityType(ModalityType.APPLICATION_MODAL);
        certShowDlg.setSize(450,500);
        certShowDlg.setVisible(true);
        certShowDlg.dispose();
    }

    public static void showCertificate(byte [] certData,String tabTitle,JPanel tabPage)
    {
        java.util.List<ECertViewerTablePageInfo> tabPages=new ArrayList<ECertViewerTablePageInfo>();
        tabPages.add(new ECertViewerTablePageInfo(tabTitle,tabPage));
        showCertificate(certData,tabPages);
    }

    public static void showCertificate(byte [] certData,java.util.List<ECertViewerTablePageInfo> tabPages)
    {
        ECertViewerDialog certShowDlg = new ECertViewerDialog(null,certData);
        for (ECertViewerTablePageInfo tabPageInfo : tabPages) {
            certShowDlg.addTabPage(tabPageInfo.getTitle(),tabPageInfo.getPagePanel());
        }
        certShowDlg.setModalityType(ModalityType.APPLICATION_MODAL);
        certShowDlg.setSize(450,500);
        certShowDlg.setVisible(true);
        certShowDlg.dispose();
    }

    public ECertViewerDialog(Frame owner,String certPath) {
        super(owner);
        initComponents();
        initGUI();
        loadCert(certPath);
    }
     public ECertViewerDialog(Frame owner,byte [] certData) {
        super(owner);
        initComponents();
         initGUI();
        loadCert(certData);
    }

    void  initGUI()
    {
        ImageIcon certViewerIcon = EImageManager.getInstance().getImageIcon("certificate_large.png", "CertViewer_Icon");
        if(certViewerIcon != null)
        {
            setIconImage(certViewerIcon.getImage());
        }
    }

    public void loadCert(String certPath) {
        eCertViewer1.loadCert(certPath);
    }

     public void loadCert(byte [] certData) {
        eCertViewer1.loadCert(certData);
    }

    public ECertViewerDialog(Dialog owner) {
        super(owner);
        initComponents();
    }

    private void eCertViewerButton1ActionPerformed(ActionEvent e) {
        setVisible(false);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        ResourceBundle bundle = Bundle.getBundle();
        eCertViewer1 = new ECertViewer();
        panel1 = new JPanel();
        eCertViewerButton1 = new ECertViewerButton();

        //======== this ========
        setTitle(bundle.getString("ECertViewer.title"));
        setResizable(false);
        setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(eCertViewer1, BorderLayout.CENTER);

        //======== panel1 ========
        {
            panel1.setLayout(new BorderLayout());

            //---- eCertViewerButton1 ----
            eCertViewerButton1.setText(bundle.getString("ECertViewerDialog.eCertViewerButton1.text"));
            eCertViewerButton1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    eCertViewerButton1ActionPerformed(e);
                    eCertViewerButton1ActionPerformed(e);
                }
            });
            panel1.add(eCertViewerButton1, BorderLayout.EAST);
        }
        contentPane.add(panel1, BorderLayout.PAGE_END);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    private void initComponentsI18n() {
        // JFormDesigner - Component i18n initialization - DO NOT MODIFY  //GEN-BEGIN:initI18n
        // JFormDesigner - End of component i18n initialization  //GEN-END:initI18n
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private ECertViewer eCertViewer1;
    private JPanel panel1;
    private ECertViewerButton eCertViewerButton1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
